package com.xkcoding.orm.mybatis.plus.serviceImpl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xkcoding.orm.mybatis.plus.entity.User;
import com.xkcoding.orm.mybatis.plus.mapper.UserMapper;

import com.xkcoding.orm.mybatis.plus.utils.MD5Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * User Service
 * </p>
 *
 * @author andrew
 * @date Created in 2021-09-17 10:17
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements IService<User> {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserServiceImpl userService;

    public PageInfo findAllUser(){
        PageHelper.startPage(1,5);
        List<HashMap> list = userMapper.findUserRole();
        PageInfo pageInfo = new PageInfo(list);
        return  pageInfo;
    }

    public List<HashMap> findAllXmlUser(){
        return  userMapper.selectAll();
    }

    public PageInfo findAllXmlUser1(){
         PageHelper.startPage(2,5);
         List<User> list = userMapper.selectList(new QueryWrapper<>());
         PageInfo pageInfo = new PageInfo(list);
        return  pageInfo;
    }

    //sql
    public void testOneInsert1(){
        long start =System.currentTimeMillis();
        List<User> list = new ArrayList<User>();
        for (int i=0;i<500;i++){
        User user = User.builder().email("andrew@qq.com").password(MD5Util.getFinalPwd(MD5Util.createSaltValue(),
                Objects.requireNonNull(MD5Util.md5EncodePwd("123456")))).salt(MD5Util.createSaltValue()).name("andrew"+i+"K")
                .phoneNumber("13719180727").status(1).createTime(DateTime.now()).lastUpdateTime(DateTime.now()).build();
       list.add(user);
        }
        for(User user:list){
        userMapper.insertOneUser(user);
        }
        long end =System.currentTimeMillis();
        System.out.println("耗费时间"+":"+(end-start));
    }

    //plus
    public void testOneInsert2(){
        long start =System.currentTimeMillis();
        List<User> list = new ArrayList<User>();
        for (int i=0;i<500;i++){
            User user = User.builder().email("andrew@qq.com").password(MD5Util.getFinalPwd(MD5Util.createSaltValue(),
                    Objects.requireNonNull(MD5Util.md5EncodePwd("123456")))).salt(MD5Util.createSaltValue()).name("andrew"+i+"K")
                    .phoneNumber("13719180727").status(1).createTime(DateTime.now()).lastUpdateTime(DateTime.now()).build();
            list.add(user);
        }
        for(User user:list){
          userService.save(user);
        }
        long end =System.currentTimeMillis();
        System.out.println("耗费时间"+":"+(end-start));
    }

    //sql
    public void testMoreInsert1(){
        long start =System.currentTimeMillis();
        List<User> list = new ArrayList<User>();
        for (int i=0;i<50000;i++){
            User user = User.builder().email("andrew@qq.com").password(MD5Util.getFinalPwd(MD5Util.createSaltValue(),
                    Objects.requireNonNull(MD5Util.md5EncodePwd("123456")))).salt(MD5Util.createSaltValue()).name("andrew"+i+"K")
                    .phoneNumber("13719180727").status(1).createTime(DateTime.now()).lastUpdateTime(DateTime.now()).build();
            list.add(user);
        }
        baseMapper.insertMoreUser(list);
        long end =System.currentTimeMillis();
        System.out.println("耗费时间"+":"+(end-start));
    }

    //sql
    public void testMoreInsert2(){
        long start =System.currentTimeMillis();
        List<User> list = new ArrayList<User>();
        for (int i=0;i<500;i++){
            User user = User.builder().email("andrew@qq.com").password(MD5Util.getFinalPwd(MD5Util.createSaltValue(),
                    Objects.requireNonNull(MD5Util.md5EncodePwd("123456")))).salt(MD5Util.createSaltValue()).name("andrew"+i+"K")
                    .phoneNumber("13719180727").status(1).createTime(DateTime.now()).lastUpdateTime(DateTime.now()).build();
            list.add(user);
        }
        userService.saveBatch(list);
        long end =System.currentTimeMillis();
        System.out.println("耗费时间"+":"+(end-start));
    }


}



